/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.msk.wlhy.service;


import com.msk.common.utils.DictUtils;
import com.msk.common.utils.StringUtils;
import com.msk.sys.service.CrudService;
import com.msk.sys.utils.createNumber;
import com.msk.wlhy.domain.*;
import com.msk.wlhy.mapper.HtTransportDetailDao;
import com.msk.wlhy.mapper.HtTransportListDao;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * []
 * </p>
 * <p>
 * 作者：zkp
 * </p>
 * <p>
 * 邮箱：zkp@xiangyou.com
 * </p>
 * <p>
 * 创建时间： 2018-06-08
 * </p>
 * <p>
 * 类说明：运输单明细管理Service
 * </p>
 * <p>
 * 修改记录：
 * </p>
 */
@Service
@Transactional(readOnly = true)
public class HtTransportDetailService extends CrudService<HtTransportDetailDao, HtTransportDetail> {
	@Autowired
	private HtLogisticsOrderService htLogisticsOrderService;
	@Autowired
	private HtLogisticsOrderDetailService htLogisticsOrderDetailService;
	@Autowired
	private HtTransportListDao htTransportListDao;
	@Autowired
	private HtTransportDetailDao htTransportDetailDao;
	@Autowired
	private HtCarFilesService htCarFilesService;
	@Autowired
	private HtDriverFileService htDriverFileService;

	public HtTransportDetail get(String id) {
		return super.get(id);
	}

	public List<HtTransportDetail> findList(HtTransportDetail htTransportDetail) {
		return super.findList(htTransportDetail);
	}
	
	public HtTransportDetail showDetailById(HtTransportDetail htTransportDetail){
		return dao.showDetailById(htTransportDetail);
	}
	


	@Transactional(readOnly = false)
	public void save(HtTransportDetail htTransportDetail) {
		if(StringUtils.isBlank(htTransportDetail.getId())){
			HtTransportList transportList = htTransportListDao.get(htTransportDetail.getTransportId());
			if(transportList != null){
				transportList.setDispatchTime(htTransportDetail.getStartDate());
				transportList.setCreateDate(new Date());
				htTransportListDao.update(transportList);
			}
		}else{
			HtTransportDetail old = get(htTransportDetail.getId());
			if(old.getStartDate() != htTransportDetail.getStartDate()){  //判断是否修改发车时间，如果修改了发车时间对应的派车时间也修改为发车时间
				HtTransportList transportList = htTransportListDao.get(htTransportDetail.getTransportId());
				transportList.setDispatchTime(htTransportDetail.getStartDate());
				transportList.setCreateDate(new Date());
				htTransportListDao.update(transportList);
			}
		}
		super.save(htTransportDetail);
	}

	@Transactional(readOnly = false)
	public void delete(HtTransportDetail htTransportDetail) {
		super.delete(htTransportDetail);
	}

	/**
	 * 该方法只针对派车单页面选择完成订单明细后插入运输单明细使用
	 *
	 * @param selectOrderDetailIds
	 * @param transportId
	 *            void
	 * @throws
	 */
	@Transactional(readOnly = false)
	public void insertData(String selectOrderDetailIds, String transportId) {
		HtTransportDetail htTransportDetailQuery = new HtTransportDetail();
		htTransportDetailQuery.setTransportId(transportId);
		List<HtTransportDetail> list = this.findList(htTransportDetailQuery);
		dao.realDelete(htTransportDetailQuery);// 首先删除明细信息
		Map<String, String> orderNoMap = new HashMap<String, String>();
		if (CollectionUtils.isEmpty(list)) {
			for (HtTransportDetail detail : list) {
				orderNoMap.put(detail.getOrderDetailId(), detail.getOrderNo());
			}
		}
		String orderDetailIds[] = selectOrderDetailIds.split(",");
		if (orderDetailIds != null && orderDetailIds.length > 0) {
			for (int i = 0; i < orderDetailIds.length; i++) {
				HtTransportDetail htTransportDetail = new HtTransportDetail();
				String orderNo = orderNoMap.get(orderDetailIds[i]);
				if (StringUtils.isEmpty(orderNo)) {
					String htCode = DictUtils.getDictLabel("YSMX", "ht_code", "");
					orderNo = createNumber.getOrderNo(htCode);
				}
				// 插入操作
				htTransportDetail = new HtTransportDetail();
				htTransportDetail.setTransportId(transportId);
				htTransportDetail.setOrderDetailId(orderDetailIds[i]);
				htTransportDetail.setOrderNo(orderNo);
				this.save(htTransportDetail);
				HtLogisticsOrderDetail htLogisticsOrderDetail = htLogisticsOrderDetailService.get(orderDetailIds[i]);// 获取订单明细对象
				if (htLogisticsOrderDetail != null && htLogisticsOrderDetail.getStatus().equals("1")) {
					htLogisticsOrderDetail.setStatus("2");
					htLogisticsOrderDetailService.save(htLogisticsOrderDetail);
				}

			}
		}
		// 修改相应订单状态为执行中
		List<HtLogisticsOrder> htLogisticsOrderList = htLogisticsOrderService.findListByDetailIds(selectOrderDetailIds);
		if (!CollectionUtils.isEmpty(htLogisticsOrderList)) {
			for (HtLogisticsOrder htLogisticsOrder : htLogisticsOrderList) {
				String status = htLogisticsOrder.getStatus();
				if (status.equals("1")) {// 订单为派车
					HtLogisticsOrder order = htLogisticsOrderService.get(htLogisticsOrder.getId());
					order.setStatus("2");// 订单执行中
					htLogisticsOrderService.save(order);
				}
			}
		}
	}

	/**
	 * 根据司机id获取对应的未打卡的运输单明细数据
	 *
	 * @param htTransportDetail
	 * @return List<HtTransportDetail>
	 * @throws
	 */
	public List<HtTransportDetail> getTransportDetailByDriverId(HtTransportDetail htTransportDetail) {
		return dao.getTransportDetailByDriverId(htTransportDetail);
	}

	/**
	 * 获取未接单的运单信息
	 *
	 * @description findTransportDetailByDriverId
	 * @date 2018-7-18
	 * @author chuming
	 * @param htTransportDetail
	 * @return
	 */
	public List<HtTransportDetail> findTransportDetailByDriverId(HtTransportDetail htTransportDetail) {
		return dao.findTransportDetailByDriverId(htTransportDetail);
	}

	@Transactional(readOnly = false)
	public void doConfirmArrival(HtTransportDetail htTransportDetail) {
		htTransportDetail.setAuditStatus("0");//未核准
		htTransportDetail.setIsBilling("0");//未支付
		this.save(htTransportDetail);
		String driverId = "";

		// 校验该运单明细对应的运输单是否已经全部打卡完成，如果全部打卡完成需要修改运单状态为已完成，对应的车辆也要修改为正常状态
		//Map<String, Object> map = this.checkTransportListIsFinish(htTransportDetail.getTransportId());
		//if (map != null) {
			//Object allcount = map.get("allcount");// 需要打卡的运单明细个数
			//Object punchCount = map.get("punchCount");// 已经打卡的运单明细个数
			//if (allcount.equals(punchCount)) {
				// 当两个数字相同时说明已经打卡完成，需要修改运单状态和车辆状态
				//Object id = map.get("id");// 运单id
				//Object carId = map.get("carId");// 运单对应的车辆信息id
				HtTransportList htTransportList = htTransportListDao.get(htTransportDetail.getTransportId());
				HtDriverFile htDriverFile = new HtDriverFile();
				if(htTransportList != null && StringUtils.isNotBlank(htTransportList.getAttstr01())){
					driverId = htTransportList.getAttstr01();
					htDriverFile = htDriverFileService.get(driverId);
				}

				if(htDriverFile != null&& "1".equalsIgnoreCase(htDriverFile.getDriverStatus())){
					htDriverFile.setDriverStatus("0");
					htDriverFileService.updatedriverStatus(htDriverFile);
				}
				htTransportList.setStatus("3");
				htTransportList.setCreateDate(new Date());;
				htTransportListDao.update(htTransportList);
				//司机上传收货单确认运抵后针对直接发布的托运单，修改对应的托运单的运输状态和到货时间
				if(StringUtils.isNotBlank(htTransportDetail.getOrderDetailId()) && "1".equals(htTransportDetail.getPublishWay())){
					HtLogisticsOrderDetail htLogisticsOrderDetail = htLogisticsOrderDetailService.get(htTransportDetail.getOrderDetailId());
					htLogisticsOrderDetail.setGoodsEndTime(new Date());
					if("2".equalsIgnoreCase(htLogisticsOrderDetail.getPriceWay())&&htLogisticsOrderDetail.getCarNum().equalsIgnoreCase(htTransportList.getDispatchCarNo())){
						htLogisticsOrderDetail.setAttstr03("2");
					}else if(StringUtils.isBlank(htLogisticsOrderDetail.getPriceWay()) || "1".equalsIgnoreCase(htLogisticsOrderDetail.getPriceWay())){
						htLogisticsOrderDetail.setAttstr03("2");
					}
					htLogisticsOrderDetailService.save(htLogisticsOrderDetail);
				}


				HtCarFiles htCarFiles = htCarFilesService.get(htTransportList.getCarId());
				htCarFiles.setStatus("1");
				if (StringUtils.isNotBlank(htTransportDetail.getCardCoordinate())) {
					String[] points = htTransportDetail.getCardCoordinate().split(",");
					if (points.length == 2) {
						htCarFiles.setLng(points[0]);
						htCarFiles.setLat(points[1]);
					}
				}
				htCarFilesService.save(htCarFiles);
				// 修改对应车牌号的所有车辆
				HtCarFiles carFiles = new HtCarFiles();
				carFiles.setCarNumber(htCarFiles.getCarNumber());
				List<HtCarFiles> list = htCarFilesService.findListAll(carFiles);

				list.remove(htCarFiles);
				for (HtCarFiles htCarFiles2 : list) {
					htCarFiles2.setStatus("1");
					htCarFiles2.setLng(htCarFiles.getLng());
					htCarFiles2.setLat(htCarFiles.getLat());
					htCarFilesService.save(htCarFiles2);
				}
			//}
		//}
		//logisticsMsgWaybillService.saveEntityByDetailId(htTransportDetail.getId());// 生成电子运单
		/*
		 * // 根据运输单明细查找对应的订单id，当订单还处于执行中时继续往下执行，如果订单已经完成就不需要继续执行了 Map<String,
		 * Object> orderMap =
		 * this.getOrderInfoBytransportDetailId(htTransportDetail.getId()); if
		 * (orderMap != null) { String orderId = orderMap.get("id") == null ? ""
		 * : orderMap.get("id").toString(); String status =
		 * orderMap.get("status") == null ? "" :
		 * orderMap.get("status").toString(); if (!status.equals("3")) { //
		 * 当前订单状态不为已完成状态，需要根据运输单的完成情况反馈订单状态 //
		 * 根据订单id反查订单下所有订单明细对应的运输单的状态，当订单明细对应的所有运输单都反馈完成时需要修改订单为已完成 Map<String,
		 * Object> checkOrderMap = this.checkOrderIsFinish(orderId); //
		 * allCount表示订单下所有订单明细对应的运输单都为完成状态时运输单个数*3的值 String allCount =
		 * checkOrderMap.get("allCount") == null ? "" :
		 * checkOrderMap.get("allCount").toString(); //
		 * realCount表示订单下所有订单明细对应的运输单现在的状态求和的值 String realCount =
		 * checkOrderMap.get("realCount") == null ? "" :
		 * checkOrderMap.get("realCount").toString(); //
		 * 当allCount=realCount这两相等时说有该订单下所有订单明细对应的运输单都完成了，此时订单需要修改完成状态 if
		 * (allCount.equals(realCount)) { HtLogisticsOrder htLogisticsOrder =
		 * htLogisticsOrderService.get(orderId);
		 * htLogisticsOrder.setStatus("3");
		 * htLogisticsOrderService.saveData(htLogisticsOrder); } } }
		 */

	}

	@Transactional(readOnly = false)
	public void doStartArrival(HtTransportDetail htTransportDetail) {
		this.save(htTransportDetail);
		// 校验该运单明细对应的运输单是否已经全部打卡完成，如果全部打卡完成需要修改运单状态为已完成，对应的车辆也要修改为正常状态
		//Map<String, Object> map = this.checkTransportListIsFinish(htTransportDetail.getTransportId());
		//if (map != null) {
			// 当两个数字相同时说明已经打卡完成，需要修改运单状态和车辆状态
			//Object id = map.get("id");// 运单id
			//Object carId = map.get("carId");// 运单对应的车辆信息id
			HtTransportList htTransportList = htTransportListDao.get(htTransportDetail.getTransportId());
			htTransportList.setStatus("2");
			htTransportList.setCreateDate(new Date());;
			htTransportListDao.update(htTransportList);
			HtCarFiles htCarFiles = htCarFilesService.get(htTransportList.getCarId());
			if (StringUtils.isNotBlank(htTransportDetail.getCardCoordinate())) {
				String[] points = htTransportDetail.getCardCoordinate().split(",");
				if (points.length == 2) {
					htCarFiles.setLng(points[0]);
					htCarFiles.setLat(points[1]);
				}
			}

			String driverId = "";
			HtDriverFile htDriverFile = new HtDriverFile();
			if(htTransportList != null && StringUtils.isNotBlank(htTransportList.getAttstr01())){
				driverId = htTransportList.getAttstr01();
				htDriverFile = htDriverFileService.get(driverId);
			}

			if(htDriverFile != null&& "0".equalsIgnoreCase(htDriverFile.getDriverStatus())){
				htDriverFile.setDriverStatus("2");
				htDriverFileService.updatedriverStatus(htDriverFile);
			}

			htCarFiles.setStatus("2");
			htCarFilesService.save(htCarFiles);
			// 修改对应车牌号的所有车辆
			HtCarFiles carFiles = new HtCarFiles();
			carFiles.setCarNumber(htCarFiles.getCarNumber());
			List<HtCarFiles> list = htCarFilesService.findListAll(carFiles);
			list.remove(htCarFiles);
			for (HtCarFiles htCarFiles2 : list) {
				htCarFiles2.setStatus("2");
				htCarFiles2.setLng(htCarFiles.getLng());
				htCarFiles2.setLat(htCarFiles.getLat());
				htCarFilesService.save(htCarFiles2);
			}
		//}

		/*HtLogisticsOrderDetail orderDetail = htLogisticsOrderDetailService.get(htTransportDetail.getOrderDetailId());
		orderDetail.setRealGoodsWeight(htTransportDetail.getGoodsWeight() + (orderDetail.getRealGoodsWeight() == null ? 0d : orderDetail.getRealGoodsWeight()));
		htLogisticsOrderDetailService.save(orderDetail);*/
		// logisticsMsgWaybillService.saveEntityByDetailId(htTransportDetail.getId());//
		// 生成电子运单
	}

	/**
	 * 校验运输单明细对应的运输单是否已经全部打卡完成
	 *
	 * @param transportListId
	 * @return Map<String,Object>
	 * @throws
	 */
	public Map<String, Object> checkTransportListIsFinish(String transportListId) {
		return dao.checkTransportListIsFinish(transportListId);
	}

	/**
	 * 根据运输单明细查找对应的订单id，当订单还处于执行中时继续往下执行，如果订单已经完成就不需要继续执行了
	 *
	 * @param transportDetailId
	 * @return Map<String,Object>
	 * @throws
	 */
	public Map<String, Object> getOrderInfoBytransportDetailId(String transportDetailId) {
		return dao.getOrderInfoBytransportDetailId(transportDetailId);
	}

	/**
	 * 根据订单id反查订单下所有订单明细对应的运输单的状态，当订单明细对应的所有运输单都反馈完成时需要修改订单为已完成
	 *
	 * @param orderId
	 * @return Map<String,Object>
	 * @throws
	 */
	public Map<String, Object> checkOrderIsFinish(String orderId) {
		return dao.checkOrderIsFinish(orderId);
	}

	/**
	 * @author llh
	 * @date 2023/7/4
	 * @return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
	 * @description 获取司机当前已完成的所有运单      
	 */
	public List<Map<String,Object>> findComplateDetailList(HtTransportDetail htTransportDetail){
		return dao.findComplateDetailList(htTransportDetail);
	}
	
	public List<Map<String,Object>> selectWaybillRevenueList(HtTransportDetail htTransportDetail, String status, String startDate, String endDate){
		return dao.selectWaybillRevenueList(htTransportDetail, status, startDate, endDate);
	}

	public HtTransportDetail findComplateDetailCount(HtTransportDetail htTransportDetail, String status, String startDate, String endDate){
		return dao.findComplateDetailCount(htTransportDetail, status, startDate, endDate);
	};

//	//司机评价
//	public Page<HtTransportDetail> findComplateDriverList(Page<HtTransportDetail> page, HtTransportDetail htTransportDetail){
//		htTransportDetail.setPage(page);
//		page.setList(dao.findComplateDriverList(htTransportDetail));
//		return page;
//	}
//
//	//货主评价
//	public Page<HtTransportDetail> findComplateMemberList(Page<HtTransportDetail> page, HtTransportDetail htTransportDetail){
//		htTransportDetail.setPage(page);
//		page.setList(dao.findComplateMemberList(htTransportDetail));
//		return page;
//	}

	/**
	 * @description updateDelFlag
	 * @date 2018-8-2
	 * @author chuming
	 * @param htTransportDetail
	 */
	@Transactional(readOnly = false)
	public void updateDelFlag(HtTransportDetail htTransportDetail) {
		dao.updateDelFlag(htTransportDetail);
	}


	/**
	 * 获取运单量
	 * @description selectWaybillDetailCountGroupbyMonth
	 * @date 2018-8-9
	 * @author chuming
	 * @param xs1  运单量系数
	 * @param xs2 运费系数
	 * @param xs3 货物量系数
	 * @return
	 */
	public List<Map<String, Object>> selectWaybillDetailCountGroupbyMonth(Integer xs1,Integer xs2,Integer xs3){
		return dao.selectWaybillDetailCountGroupbyMonth(xs1, xs2, xs3);
	}

	/**
	 * 获取运单量
	 * @description selectWaybillDetailCountGroupbyDay
	 * @date 2018-8-9
	 * @author chuming
	 * @param xs1  运单量系数
	 * @param xs2 运费系数
	 * @param xs3 货物量系数
	 * @return
	 */
	public List<Map<String, Object>> selectWaybillDetailCountGroupbyDay(Integer xs1,Integer xs2,Integer xs3){
		return dao.selectWaybillDetailCountGroupbyDay(xs1, xs2, xs3);
	}


	/**
	 * 获取所有的路线
	 * @description findAllLinesFormOrder
	 * @date 2018-8-9
	 * @author chuming
	 * @return
	 */
	public List<Map<String, Object>> findAllLinesFormOrder(){
		return dao.findAllLinesFormOrder();
	}


	/**
	 * 获取各个部门订单详情的名次
	 * @description getOrderNounByType
	 * @date 2018-8-9
	 * @author chuming
	 * @return
	 */
	public List<Map<String, Object>> getOrderNounByType(){
		return dao.getOrderNounByType();
	}
	

//	/**
//	 * @description findAuditPage
//	 * @date 2018-10-25
//	 * @author chuming
//	 * @param page
//	 * @param htTransportDetail
//	 * @return
//	 */
//	public Page<HtTransportDetail> findAuditPage(Page<HtTransportDetail> page, HtTransportDetail htTransportDetail) {
//		htTransportDetail.setPage(page);
//		User u = UserUtils.getUser();
//		if(u != null && u.getUserIdentity() != null && "2".equalsIgnoreCase(u.getUserIdentity())){
//			htTransportDetail.setAttstr01(u.getId());
//		}
//		page.setList(dao.findAllAuditList(htTransportDetail));
//		return page;
//	}



	/**
	 * 获取各个部门运单数量
	 * @description findOrderNumberByOfficeAndDay
	 * @date 2018-10-25
	 * @author chuming
	 * @return
	 */
	public List<Map<String, Object>> findOrderNumberByOfficeAndDay(HtTransportDetail htTransportDetail){
		return dao.findOrderNumberByOfficeAndDay(htTransportDetail);
	}

	/**
	 * 获取所有的运单数量
	 * @description findAllOrderNumberByDay
	 * @date 2018-10-25
	 * @author chuming
	 * @return
	 */
	public List<Map<String, Object>> findAllOrderNumberByDay(HtTransportDetail htTransportDetail){
		return dao.findAllOrderNumberByDay(htTransportDetail);
	}

	/**
	 * @description findOrderLinesGroupByOffice
	 * @date 2018-10-26
	 * @author chuming
	 * @param htTransportDetail
	 * @return
	 */
	public List<Map<String, Object>> findOrderLinesGroupByOffice(HtTransportDetail htTransportDetail) {
		return dao.findOrderLinesGroupByOffice(htTransportDetail);
	}

	/**
	 * @description updateData
	 * @date 2018-10-26
	 * @author chuming
	 * @param htTransportDetail
	 * @return
	 */
	@Transactional(readOnly = false)
	public int updateData(HtTransportDetail htTransportDetail){
		return dao.updateData(htTransportDetail);
	}

	/**
	 * @description getTransport
	 * @date 2018-10-26
	 * @author chuming
	 * @param id
	 * @return
	 */
	public HtTransportDetail getTransport(String id){
		return dao.getTransport(id);
	}

	/**
	 * @description getTransport
	 * @date 2018-10-26
	 * @author chuming
	 *
	 * @return
	 */
	public List<Map<String, Object>> getBill(String id){
		return dao.getBill(id);
	}

	/**
	 * @description getDetailId
	 * @date 2018-10-26
	 * @author chuming
	 *
	 * @return
	 */
	public Map<String, String> getDetailId(String id){
		return dao.getDetailId(id);
	}

	/**
	 * @description getTransport
	 * @date 2018-10-26
	 * @author chuming
	 *
	 * @return
	 */
	public List<HtTransportDetail> findDetailList(HtTransportDetail htTransportDetail){
		return dao.findDetailList(htTransportDetail);
	}

	/**
	 * @description findListCount
	 * @date 2018-10-26
	 * @author chuming
	 *
	 * @return List
	 */
	public List<HtTransportDetail> findListCount(HtTransportDetail htTransportDetail){
		return dao.findListCount(htTransportDetail);
	}

//	/* *
//	 * @Author pzk
//	 * @Description 应付申请 结算 事务
//	 * @Date 15:49 2019/8/28
//	 * @Param [htTransportDetail]
//	 * @return void
//	 **/
//	@Transactional(readOnly = false)
//	public void applyTranscation(LogisticsFundsFlowApply logisticsFundsFlowApply,String[] idList,String vehicleOwnership) {
//		logisticsFundsFlowApply.preInsert();
//		logisticsFundsFlowApplyDao.insert(logisticsFundsFlowApply);
//		String newApplyMoney = logisticsFundsFlowApply.getNewApplyMoney().toString();
//		//自有车辆均分金额
//		Double money = Double.valueOf(newApplyMoney)/(idList.length);
//		for (String id : idList) {
//			HtTransportDetail htTransportDetail = dao.get(id);
//			if(htTransportDetail != null){
//				htTransportDetail.setAuditType("2"); // 待结算
//				htTransportDetail.setApplyId(logisticsFundsFlowApply.getId());
//				//若为自有车辆
//				if("自有车辆".equals(vehicleOwnership)){
//					htTransportDetail.setAllFreight1(money);
//				}else{
//					htTransportDetail.setAllFreight1(htTransportDetail.getAllFreight());
//				}
//				// 设置通用属性
//				htTransportDetail.preUpdate();
//				dao.update(htTransportDetail);
//			}
//		}
//	}

	public HtLogisticsFrame findFrameContract(String orderDetailId){
		return dao.findFrameContract(orderDetailId);
	}

	/* *
	 * @Author cmy
	 * @Description 上传保单
	 * @Param htTransportDetail
	 * @return void
	 **/
	@Transactional(readOnly = false)
	public void uploadInsuranceFile(HtTransportDetail htTransportDetail){
		htTransportDetail.preUpdate();
		dao.uploadInsuranceFile(htTransportDetail);
	}
	
	/**
	 * @author llh
	 * @date 2023/5/24
	 * @return void
	 * @description 上传发货单及收货单      
	 */
	@Transactional(readOnly = false)
	public void uploadInvoiceOrReceipt(HtTransportDetail htTransportDetail){
		htTransportDetail.preUpdate();
		dao.uploadInvoiceOrReceipt(htTransportDetail);
	}

	public HtTransportDetail findDataforPrint(HtTransportDetail htTransportDetail){
		return dao.findDataforPrint(htTransportDetail);
	}

	public HtTransportDetail getAllFreightPrice(HtTransportDetail htTransportDetail){
		return dao.getAllFreightPrice(htTransportDetail);
	}

	public List<HtTransportDetail> getHtTransportDetailCount(String[] ids){
		return dao.getHtTransportDetailCount(ids);
	}

	/* *
	 * @Author cmy
	 * @Description 上传保单
	 * @Param htTransportDetail
	 * @return void
	 **/
	@Transactional(readOnly = false)
	public boolean delArrival(HtTransportDetail htTransportDetail){
		//获取运输单明细
		HtTransportList htTransportList = htTransportListDao.get(htTransportDetail.getTransportId());
		//获取车队合同
		HtLogisticsOrderDetail htLogisticsOrderDetail = htLogisticsOrderDetailService.get(htTransportDetail.getOrderDetailId());
		if(htLogisticsOrderDetail != null ){
			HtLogisticsOrderDetail htLogisticsOrderDetail1 = new HtLogisticsOrderDetail();
			htLogisticsOrderDetail1.setOrderId(htLogisticsOrderDetail.getOrderId());
			List<HtLogisticsOrderDetail>  htLogisticsOrderDetails = htLogisticsOrderDetailService.findList(htLogisticsOrderDetail1);
			if(htLogisticsOrderDetails.size() > 0){
				Boolean isLast = true;
				for(HtLogisticsOrderDetail hod : htLogisticsOrderDetails){
					//判断是否是最后一辆车
					if(hod != null && hod.getSupplier() == null){
						//不是最后一辆，把未派车的执行单
						double goodsWeight = htTransportDetail.getGoodsWeight() + hod.getGoodsWeight();
						hod.setGoodsWeight(goodsWeight);
						//修改空执行单数据
						htLogisticsOrderDetailService.save(hod);
						//删除运单数据
						htLogisticsOrderDetailService.delete(htLogisticsOrderDetail);
						htTransportListDao.delete(htTransportList);
						this.save(htTransportDetail);
						dao.delete(htTransportDetail);
						isLast = false;
						break;
					}
				}

				if(isLast){
					//删除运单数据
					//不是最后一辆，把未派车的执行单
					double goodsWeight = htTransportDetail.getGoodsWeight();
					htLogisticsOrderDetail.setGoodsWeight(goodsWeight);
					htLogisticsOrderDetail.setSupplier(null);
					htLogisticsOrderDetail.setRealGoodsWeight(0.00);
					htLogisticsOrderDetailService.save(htLogisticsOrderDetail);
					this.save(htTransportDetail);
					htTransportListDao.delete(htTransportList);
					dao.delete(htTransportDetail);
				}

				return true;
			}

		}else{
			return false;
		}
		return false;
	}

	/**
	 * 根据条件获取运单数量
	 * @param htTransportDetail
	 * @return
	 */
	public long getCount(HtTransportDetail htTransportDetail){
		return  dao.findCount(htTransportDetail);
	}

	public List<Map<String,Object>> findComplateDriverList(HtTransportDetail htTransportDetail){
		return dao.findComplateDriverList(htTransportDetail);
	}
	
	public Long findCount(HtTransportDetail htTransportDetail){
		return dao.findCount(htTransportDetail);
	}

	public HtTransportDetail selectDetailsById(HtTransportDetail htTransportDetail){
		return dao.selectDetailsById(htTransportDetail);
	}

	public List<Map<String, Object>> ofCarrierRevenuesCount (String supplierUser, String status, String startDate, String endDate){
		return htTransportDetailDao.ofCarrierRevenuesCount(supplierUser,status,startDate,endDate);
	}

	public List<Map<String, Object>> findListOfCarrierRevenues (String supplierUser, String status, String startDate, String endDate){
		return htTransportDetailDao.listOfCarrierRevenues(supplierUser,status,startDate,endDate);
	}

	/**
	 * @author llh
	 * @date 2023/6/20
	 * @return java.util.List<com.msk.wlhy.domain.HtTransportDetail>
	 * @description 货主查询在途运单列表
	 */
	public List<Map<String,Object>> findGoodsWayBillList(HtTransportDetail htTransportDetail){
		return htTransportDetailDao.findGoodsWayBillList(htTransportDetail);
	}

	/**
	 * @author llh
	 * @date 2023/6/20
	 * @return com.msk.wlhy.domain.HtTransportDetail
	 * @description 获取货主在途运单详情
	 */
	public HtTransportDetail getGoodsWayBillInfo(String id){
		return htTransportDetailDao.getGoodsWayBillInfo(id);
	}
	
	/**
	 * @author llh
	 * @date 2023/7/4
	 * @return java.util.Map<java.lang.String,java.lang.Object>
	 * @description 根据运单号获取运单起始经纬度信息      
	 */
	public Map<String,Object> getLngAndLatInfo(String shippingNoteNumber){
		return htTransportDetailDao.getLngAndLatInfo(shippingNoteNumber);
	}

	public Map<String,Object> selectHtTransportDetail(HtTransportDetail htTransportDetail){
		return htTransportDetailDao.selectHtTransportDetail(htTransportDetail);
	}
	
	/**
	 * @author llh
	 * @date 2023/7/13
	 * @return int
	 * @description 更新SDK行程状态      
	 */
	@Transactional(readOnly = false)
	public int updateItinerary(HtTransportDetail htTransportDetail){
		return htTransportDetailDao.updateItinerary(htTransportDetail);
	}
}